package org.vectrics.skill.struts;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.IMG;
import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.util.StringUtils;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.skill.Skill;
import org.vectrics.skill.SkillScoringMethod;

abstract public class SkillTreeTagAbstract extends TagSupport {

	private static final long serialVersionUID = 1227837142269651143L;

	private static Category log = Category.getInstance(SkillTreeTagAbstract.class);
    private Job job = null;
    public static String ENTRY_TYPE_PARAM = "entry_type";
    public static String JOB_PARAM = "job";
    private String entryTypeParam = "";

    public SkillTreeTagAbstract() {
    }

    abstract protected String getSkillsEntryHref();
    
    public int doStartTag() throws JspException  {
    	log.debug("doStartTag() called");
    	HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
    	String contextPath = request.getContextPath();
    	String entryTypeParam = request.getParameter(ENTRY_TYPE_PARAM);

        String jobId = request.getParameter(JOB_PARAM);
        log.debug("doRender() - geting content for skills table.");
        String html =  "<br>" + getSkillTable(); 
        
        JspWriter writer = pageContext.getOut();
        try {
        	writer.print(html.toString());
        }
        catch (IOException ex) {
        	throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
        }
        return(EVAL_PAGE);
        
    }
    
    
    
    public String getSkillTable() throws JspException
    {   
    	
    	Skill rootSkill = null;
  		rootSkill = RecruitServiceLocator.getSkillProvider().getRootSkill();
    	
    	
        BorderTable table = new BorderTable();

        table.setCellSpacing(0);
        table.setWidth(1000);

        Row row1 = table.addRow();
        row1.setBgColor("#003040");
        //Cell cell1 = row1.addCell();
        //cell1.addElement(TextHelper.getFont("stdBoldFont", "Category"));

        Cell cell2 = row1.addCell();
        cell2.addElement(TextHelper.getFont("stdBoldFont", "Description"));

        Cell cell3 = row1.addCell();
        cell3.addElement(TextHelper.getFont("stdBoldFont", "Code"));

        Cell cell4 = row1.addCell();
        cell4.addElement(TextHelper.getFont("stdBoldFont", "Rating"));

        try {
            show(rootSkill, table, 0);
        }
        catch (Exception ex) {
            log.error("Exception displaying user skills.", ex);
            throw new JspException("Exception displaying user skills.", ex);
        }

        return(table.toString());
    };


    void show(Skill parent, Table table, int indent) {
        if (log.isDebugEnabled())
            log.debug("SkillTree()  parent: " + parent.getCode());
        Iterator iter = parent.getChildren().iterator();
        while (iter.hasNext()) {
            Skill childSkill = (Skill)iter.next();
            showSkill(childSkill, table, indent);
            show(childSkill, table, indent + 1);
        }
        
    }
    
    void showSkill(Skill skill, Table table, int indent) {
        
        int redBg = 255 - (indent * 8);
        int greenBg = 255 - (indent * 6);
        int blueBg = 255 - (indent * 8);
        
        //String bgColor = org.vectrics.common.html.Color.makeColor(redBg, greenBg, blueBg);
        String bgColor= "#ffffff";
        //log.info("Bg Color = " + bgColor);
        
        if (log.isDebugEnabled())
            log.debug("showSkill() called.  Skill = " + skill.getCode());
        log.debug("showSkill() - making row for skill");
        Row row2 = table.addRow();
        //Cell categoryCell = row2.addCell();
        Cell descCell = row2.addCell();
        descCell.setBgColor(bgColor);
        descCell.setClass("");
        
        Cell codeCell = row2.addCell();
        codeCell.setBgColor(bgColor);
        codeCell.setClass("");
        
        Cell ratingCell = row2.addCell();
        ratingCell.setBgColor(bgColor);
        ratingCell.setClass("");

        Table descTable = new Table();
        descTable.setCellPadding(0);
        descTable.setCellSpacing(0);
        descTable.setBorder(0);
        Row descRow = descTable.addRow();

        log.debug("showSkill() - building indention for skill rating on hierarchy.");
        for (int i = 0; i < indent; i++) {
            Cell indentCell = descRow.addCell();
            indentCell.addElement(makeIndentImage());
        }
        
        IMG img = new IMG();
        img.setSrc("/recruit/graphics/med_dot.gif");
        img.setAlt("");
        img.setBorder(0);
        img.setHeight(4);
        img.setWidth(4);
        
        Cell iconCell = descRow.addCell();
        iconCell.setVAlign("middle");
        iconCell.addElement(img);
        iconCell.addElement("&nbsp;");
        
        log.debug("showSkill() - adding skill desc to cell.");
        //categoryCell.addElement(TextHelper.getFont("stdFont", indentStr); //categoryText));
        descRow.addCell().addElement(TextHelper.getFont("subFont", skill.getDescription()));
        descCell.addElement(descTable);
        descCell.setClass("");
        descCell.setBgColor(bgColor);
        
        log.debug("showSkill() - adding skill rating description to cell.");
        String inputDesc = "";
        if (skill.getScoringMethod().equals(SkillScoringMethod.SCORING_METHOD_YES_NO)) {
            if (inputDesc.length() > 0)
                inputDesc += "&nbsp;-&nbsp;";
            inputDesc += "Yes/No";
        //} else if (skill.getScoringMethod().equals(SkillScoringMethod.SCORING_METHOD_YEARS)) {
        //    if (inputDesc.length() > 0)
        //        inputDesc += "&nbsp;-&nbsp;";
        //    inputDesc += "Years";
        } else if (skill.getScoringMethod().getCode().equals(SkillScoringMethod.SCORING_METHOD_LEVEL)) { 
            if (inputDesc.length() > 0)
                inputDesc += "&nbsp;-&nbsp;";
            inputDesc += "Level";
        }
        
        ratingCell.addElement(TextHelper.getFont("subFont", inputDesc));
        String code = skill.getCode();
        if (code.startsWith("root.")) {
            code = code.substring(5);
        }
        if (StringUtils.isNullOrBlank(code)) {
        	code = "&nbsp;";
        }
        codeCell.addElement(TextHelper.getFont("subFont", code));
        log.debug("showSkill() - done.");
    }
    
    private IMG makeIndentImage() {
        
       IMG img = new IMG();
       img.setSrc("/recruit/graphics/cleardot.gif");
       img.setAlt("");
       img.setBorder(0);
       img.setHeight(10);
       img.setWidth(25);
       return(img);
    }

}